package org.yunmai.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.yunmai.dto.DeptPageQuery;
import org.yunmai.entity.SysDept;
import org.yunmai.service.SysDeptService;
import org.yunmai.util.R;
import org.yunmai.vo.PageResult;
import org.yunmai.vo.SysDeptVO;

import java.util.List;

/**
 * @Author 李孝辉
 * @Date 2025/6/3 22:16
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    @Resource
    SysDeptService sysDeptService;
    @GetMapping("/showAll")
    public R<List<SysDept>> showAll() {
        return R.success(sysDeptService.list());
    }

    @PostMapping("/getById")
    public R<SysDept> getById(Integer id) {
        return R.success(sysDeptService.getById(id));
    }

    @DeleteMapping("/deleteById/{id}")
    public R<Boolean> deleteById(@PathVariable Integer id) {
        return R.success(sysDeptService.removeById(id));
    }

    @PutMapping("/update/{id}")
    public R<Boolean> update(@RequestBody SysDept sysDept) {
        if (sysDept.getId() == null) {
            return R.error("id不能为空");
        }
        boolean result = sysDeptService.updateById(sysDept);
        return R.success(result);
    }
    @PostMapping("/PagingList")
    public R<PageResult<SysDeptVO>> getDeptPageList(@RequestBody DeptPageQuery query){
        List<SysDeptVO> deptVOS=sysDeptService.getDeptPage(query.getPageNum(), query.getPageSize(), query.getKeyword());
        int total=deptVOS.toArray().length;
        PageResult<SysDeptVO> result = new PageResult<>();
        result.setList(deptVOS);
        result.setTotal(total);
        return R.success(result);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody SysDept sysDept) {
        boolean result = sysDeptService.save(sysDept);
        return R.success(result);
    }
}
